package com.hkts.naturalgas.controller;

import com.hkts.naturalgas.entity.Book;
import com.hkts.naturalgas.feign.TestInterface;
import com.hkts.naturalgas.service.BookService;
import com.hkts.naturalgas.utils.NaturalgasConstant;
import com.hkts.naturalgas.utils.NaturalgasIndex;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 *
 */
@Slf4j
@RestController

@RequestMapping("/naturalgas/hkAleterLevel")
@Transactional
public class HkAleterLevelController {
    @Autowired
    private BookService bookService;
    @Autowired
    private TestInterface testInterface;

    /**
     *
     * @return
     */
    @RequestMapping("/testFeign")
    public Map<String, Object> testFeign() {
        return testInterface.test();
    }

    @RequestMapping("/list")
    public List<Book> list() {
        List<Book> findall = bookService.findall();
        return findall;
    }

    @RequestMapping("/naturalgasData")
    public Map<String, Object> naturalgasData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        List<String> naturalgasFilter = NaturalgasIndex.getNaturalgasFilter();
        List<String> naturalgasHeating = NaturalgasIndex.getNaturalgasHeating();
        List<String> naturalgasMeasurement = NaturalgasIndex.getNaturalgasMeasurement();
        List<String> naturalgasPressure = NaturalgasIndex.getNaturalgasPressure();
        List<String> strings = NaturalgasIndex.ininteParamters();
        List<String> strings1 = NaturalgasIndex.ininteSignalType();
        Map<String, Object> mapFilter = new HashMap<String, Object>();
        // List<Map<String, Object>> listData=new ArrayList<Map<String, Object>>();
        for (int i = 0; i < naturalgasFilter.size(); i++) {
            String[] split = naturalgasFilter.get(i).split(":");
            String[] split1 = split[1].split(",");
            List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < split1.length; j++) {
                Map<String, Object> mapParam = new HashMap<>();
                mapParam.put("paramName", strings.get(Integer.parseInt(split1[j])));
                mapParam.put("signalType", strings1.get(Integer.parseInt(split1[j])));
                mapParam.put("value", getValuebySignalType(strings1.get(Integer.parseInt(split1[j]))));
                listData.add(mapParam);
            }
            Map contentMap = new HashMap();
            contentMap.put("left", i);
            contentMap.put("top", i + 2);
            contentMap.put("param", listData);
            mapFilter.put(split[0], contentMap);
        }
        //加热
        Map<String, Object> mapMeasurement = new HashMap<String, Object>();
        for (int i = 0; i < naturalgasHeating.size(); i++) {
            String[] split = naturalgasHeating.get(i).split(":");
            String[] split1 = split[1].split(",");
            List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < split1.length; j++) {
                Map<String, Object> mapParam = new HashMap<>();
                mapParam.put("paramName", strings.get(Integer.parseInt(split1[j])));
                mapParam.put("signalType", strings1.get(Integer.parseInt(split1[j])));
                mapParam.put("value", getValuebySignalType(strings1.get(Integer.parseInt(split1[j]))));
                listData.add(mapParam);
            }
            Map contentMap = new HashMap();
            contentMap.put("left", i);
            contentMap.put("top", i + 2);
            contentMap.put("param", listData);
            mapMeasurement.put(split[0], contentMap);
        }
        mapFilter.putAll(mapMeasurement);
        //计量
        Map<String, Object> mapPressure = new HashMap<String, Object>();
        for (int i = 0; i < naturalgasPressure.size(); i++) {
            String[] split = naturalgasPressure.get(i).split(":");
            String[] split1 = split[1].split(",");
            List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < split1.length; j++) {
                Map<String, Object> mapParam = new HashMap<>();
                mapParam.put("paramName", strings.get(Integer.parseInt(split1[j])));
                mapParam.put("signalType", strings1.get(Integer.parseInt(split1[j])));
                mapParam.put("value", getValuebySignalType(strings1.get(Integer.parseInt(split1[j]))));
                listData.add(mapParam);
            }
            Map contentMap = new HashMap();
            contentMap.put("left", i);
            contentMap.put("top", i + 2);
            contentMap.put("param", listData);
            mapPressure.put(split[0], contentMap);
        }
        mapFilter.putAll(mapPressure);
        return mapFilter;
    }

    //获取信号
    public Object getValuebySignalType(String signalType) {

        if (signalType.equals("AI")) {
            int i = (int)(Math.random()*100+1);
            return i;
        } else if (signalType.equals("DI")) {
            return Math.random() > 0.5 ? 1 : 0;
        }
        return "数据有问题";
    }

    //过滤区Filter
    public Map<String, Object> filterData() {
        Map<String, Object> map = new HashMap<String, Object>();

        return map;
    }
}
